home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Toolbox / PopUpMenuWithCurFont / PopUpMenuWithCurFont.c < prev    next >
Encoding:
Text File  |  1997-02-27  |  5.3 KB  |  230 lines  |  [TEXT/CWIE]

  1.     //
  2.     //    PopUpMenuSelectWithCurFont demonstrates which low memory globals
  3.     //    (and possibly even nastier things) one must twiddle in order to
  4.     //    control the font used by MDEF 0 during PopUpMenuSelect. Note that
  5.     //    aside from the compatibility code associated with HOSTED_BY_FONT_MISCREANT,
  6.     //    this code roughly parallels what the popup menu CDEF (ID 63) does.
  7.     //    If at all possible you should make use of the CDEF, because when
  8.     //    Engineering breaks the low memory trick, we can fix the CDEF, but
  9.     //    we can't fix your code.
  10.     //
  11.     //    Complaints and kudos to:
  12.     //
  13.     //        Pete Gontier
  14.     //        Apple Macintosh Developer Technical Support
  15.     //        <gurgle@apple.com>
  16.     //
  17.     //    Change history:
  18.     //
  19.     //        03/25/96    PG        Was calling DeleteMenu with magic number.
  20.     //
  21.     //        03/25/96    PG        Fixed bizarre menu bar offset problem.
  22.     //                            Thanks to Harold Ekstrom.
  23.     //
  24.     //        02/26/97    PG        Somehow the files on the CD had become
  25.     //                            confused. Verified everything still works.
  26.     //
  27.  
  28. #define SystemSevenOrLater        1
  29. #define CGLUESUPPORTED            0
  30. #define OLDROUTINENAMES            0
  31. #define OLDROUTINELOCATIONS        0
  32.  
  33. #ifndef __FONTS__
  34. #    include <Fonts.h>
  35. #endif
  36.  
  37. #ifndef __DIALOGS__
  38. #    include <Dialogs.h>
  39. #endif
  40.  
  41. #ifndef __RESOURCES__
  42. #    include <Resources.h>
  43. #endif
  44.  
  45. #ifndef __LOWMEM__
  46. #    include <LowMem.h>
  47. #endif
  48.  
  49.     //
  50.     //    HOSTED_BY_FONT_MISCREANT
  51.     //
  52.     //    If you are writing an external code resource (filter,
  53.     //    XCMD, etc.), and this code isn't giving you any joy,
  54.     //    let HOSTED_BY_FONT_MISCREANT be 1 and see if that helps.
  55.     //    Some extension hosts set up a hostile font environment.
  56.     //    Your lack of joy is their fault, and they force you
  57.     //    to assume even more compatibility risk than you would
  58.     //    by using this code without HOSTED_BY_FONT_MISCREANT
  59.     //    defined. On the other hand, we *are* weaving a tangled
  60.     //    web by twiddling low memory, and sometimes one must pay
  61.     //    the price for this sort of behavior.
  62.     //
  63.  
  64. #define HOSTED_BY_FONT_MISCREANT 0
  65.  
  66. #if HOSTED_BY_FONT_MISCREANT
  67.  
  68. static pascal GrafPtr GetSomeWindowManagerPort (void)
  69. {
  70.     //
  71.     //    Produces a pointer to the Window Manager port or
  72.     //    Color Window Manager Port. You can test the returned
  73.     //    GrafPtr to see whether it's colored or not, but
  74.     //    many callers won't care.
  75.     //
  76.  
  77.     GrafPtr result = nil;
  78.  
  79.     SysEnvRec theWorld;
  80.  
  81.     //
  82.     //    Yes, SysEnvirons is deprecated. But it still works,
  83.     //    and it's a cheap way to buy backward compatibility
  84.     //    if you only need to know whether CQD support exists.
  85.     //
  86.  
  87.     if (SysEnvirons (1, &theWorld))
  88.         DebugStr ("\pWhoa! Panic! SysEnvirons failed?!");
  89.     else if (theWorld.hasColorQD)
  90.         GetCWMgrPort ((CGrafPtr *) &result);
  91.     else
  92.         GetWMgrPort (&result);
  93.  
  94.     return result;
  95. }
  96.  
  97. #endif
  98.  
  99. static pascal void PopUpMenuSelectWithCurFont
  100.     (MenuRef popUpMenuRef, Point where, unsigned short prevSelection)
  101. {
  102.     GrafPtr hostPort;
  103.  
  104.     short oldSysFont = LMGetSysFontFam ( );
  105.     short oldSysSize = LMGetSysFontSize ( );
  106.  
  107.     GetPort (&hostPort);
  108.  
  109.     //
  110.     //    Believe it or not, it's important to insert
  111.     //    the menu before diddling the font characteristics.
  112.     //
  113.  
  114.     InsertMenu (popUpMenuRef,hierMenu);
  115.  
  116.     LMSetSysFontFam (hostPort->txFont);
  117.     LMSetSysFontSize (hostPort->txSize);
  118.     LMSetLastSPExtra (-1);
  119.  
  120. #if HOSTED_BY_FONT_MISCREANT
  121.     SetPort (GetSomeWindowManagerPort ( ));
  122.     TextFont (0);
  123.     TextSize (0);
  124.     SetPort (hostPort);
  125. #endif
  126.  
  127.     PopUpMenuSelect (popUpMenuRef, where.v, where.h, prevSelection);
  128.  
  129.     LMSetSysFontFam (oldSysFont);
  130.     LMSetSysFontSize (oldSysSize);
  131.     LMSetLastSPExtra (-1);
  132.  
  133.     DeleteMenu ((**popUpMenuRef).menuID);
  134. }
  135.  
  136. //////////////////////////////////////////////////////////////////////
  137. //
  138. //    Below please find the usual sort of application boilerplate.
  139. //
  140. //////////////////////////////////////////////////////////////////////
  141.  
  142. static pascal OSErr InitMac (void)
  143. {
  144.     MaxApplZone ( );
  145.     InitGraf (&(qd.thePort));
  146.     InitFonts ( );
  147.     InitWindows ( );
  148.     InitMenus ( );
  149.     TEInit ( );
  150.     InitDialogs (nil);
  151.  
  152.     return noErr;
  153. }
  154.  
  155. static pascal Boolean ModalFilterProc (DialogPtr theDialog, EventRecord *theEvent, short *)
  156. {
  157.     Boolean result = false;
  158.  
  159.     if (theEvent->what == mouseDown)
  160.     {
  161.         Point localWhere = theEvent->where;
  162.         GrafPtr savePort;
  163.     
  164.         GetPort (&savePort);
  165.         SetPort (theDialog);
  166.         GlobalToLocal (&localWhere);
  167.     
  168.         if (FindDialogItem (theDialog, localWhere) == 1)
  169.         {
  170.             MenuRef popUpMenuRef = GetMenu (128);
  171.             if (popUpMenuRef)
  172.             {
  173.                 short txFont = theDialog->txFont;
  174.                 short txSize = theDialog->txSize;
  175.                 short iType; Handle iHandle; Rect iRect;
  176.                 Point popWhere;
  177.  
  178.                 GetDialogItem (theDialog,2,&iType,&iHandle,&iRect);
  179.  
  180.                 popWhere.v = iRect.bottom;
  181.                 popWhere.h = iRect.left + 2;
  182.                 LocalToGlobal (&popWhere);
  183.  
  184.                 InvertRect (&iRect);
  185.                     TextFont (geneva);
  186.                         TextSize (9);
  187.                             PopUpMenuSelectWithCurFont (popUpMenuRef,popWhere,0);
  188.                         TextSize (txSize);
  189.                     TextFont (txFont);
  190.                 InvertRect (&iRect);
  191.  
  192.                 ReleaseResource ((Handle) popUpMenuRef);
  193.             }
  194.         }
  195.     
  196.         SetPort (savePort);
  197.     }
  198.  
  199.     return result;
  200. }
  201.  
  202. static pascal Boolean SetUpMenuBar (void)
  203. {
  204.     Boolean result = false;
  205.     Handle mBar = GetNewMBar (128);
  206.     if (!ResError ( ) && mBar)
  207.     {
  208.         SetMenuBar (mBar);
  209.         AppendResMenu (GetMenuHandle (130), 'DRVR');
  210.         DrawMenuBar ( );
  211.         result = true;
  212.         ReleaseResource (mBar);
  213.     }
  214.     return result;
  215. }
  216.  
  217. void main (void)
  218. {
  219.     if (!InitMac ( ) && SetUpMenuBar ( ))
  220.     {
  221.         DialogRef dlgRef = GetNewDialog (128,nil,nil);
  222.         if (dlgRef)
  223.         {
  224.             short itemHit;
  225.             ModalDialog (NewModalFilterProc(ModalFilterProc),&itemHit);
  226.             DisposeDialog (dlgRef);
  227.         }
  228.     }
  229. }
  230.